/***********************************************************************
	filename: 	DaterGroupManager.h
	created:	6/5/2012
	author:		4Ys
	
	purpose:	Implements the interface for the GroupManager object
*************************************************************************/

#include "DaterGroupManager.h"

namespace Dater
{
	//----------------------------------------------------------------------------//
	// singleton instance pointer
	template<> GroupManager* Singleton<GroupManager>::ms_Singleton	= 0;
	
	//----------------------------------------------------------------------------//
	GroupManager::GroupManager(void)
	{
		//get _uid_counter from database
	}
	
	//----------------------------------------------------------------------------//
	GroupManager::~GroupManager(void)
	{
		destroyAllGroups();
	}
	
	//----------------------------------------------------------------------------//
	Group* GroupManager::addGroup(const std::string& name,const std::string& type,const std::string& decription, int gID, const Time& createTime
		, int creatorID)
	{
		if(isGroupPresent(gID))
		{
			return getGroup(gID);
		}

		Group* newGroup = new Group(gID);
		newGroup->setName(name);
		newGroup->setDescription(decription);
		newGroup->setType(type);
		newGroup->setCreateTime(createTime);
		newGroup->setCreatorID(creatorID);

		_groupRegistry[gID] = newGroup;

		return newGroup;
	}
	//----------------------------------------------------------------------------//
	void GroupManager::destroyAllGroups(void)
	{
		/*
		std::string group_name;
		while (!_groupRegistry.empty())
		{
			group_name = _groupRegistry.begin()->first;
			destroyGroup(group_name);
		}
		*/
		int group_id;
		while(!_groupRegistry.empty())
		{
			group_id = _groupRegistry.begin()->first;
			destroyGroup(group_id);
		}
	}
	
	//----------------------------------------------------------------------------//
	void GroupManager::destroyGroup(int groupID)
	{
		GroupRegistry::iterator gropos = _groupRegistry.find(groupID);
		if (gropos != _groupRegistry.end())
		{
			Group* gro = gropos->second;
			_groupRegistry.erase(gropos);
			
			delete gro;
		}
	}
	
	//----------------------------------------------------------------------------//
	Group* GroupManager::getGroup(int groupID) const
	{
		GroupRegistry::const_iterator pos = _groupRegistry.find(groupID);

		if (pos == _groupRegistry.end())
		{
			//exception! the event does not exist.
		}
		return pos->second;
	}


	//----------------------------------------------------------------------------//
	bool GroupManager::isGroupPresent(int groupID) const
	{
		return (_groupRegistry.find(groupID) != _groupRegistry.end());
	}


	GroupManager::GroupIterator GroupManager::getIterator(void) const
	{
		return GroupIterator(_groupRegistry.begin(), _groupRegistry.end());
	}
}